<template>
	<view class="entire_mid">
		<cu-custom bgColor="bg-Midnight-Blue">
			<block style="color: #fff;" slot="backText" @click="cancellation">取消</block>
			<block slot="content" style="color: #fff;">发帖子</block>
		</cu-custom>
		<!-- <view style="padding: 80rpx 0 0 30rpx;">
			<input type="text" v-model="title" placeholder="文章标题" style="color: #fff;" />
		</view> -->
		<view class="cu-form-group margin-top" style="background-color: #112858;color: #fff;">
			<textarea maxlength="-1" v-model="content" :disabled="modalName!=null" @input="textareaAInput"
				placeholder="分享新鲜事..." style="height: 800rpx;"></textarea>
		</view>

		<view class="cu-bar bg-white margin-top">
			<view class="action">
				文件上传
			</view>
			<view class="action">
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage"
					:data-url="item[0].url">
					<image :src="item[0].url" mode="aspectFill" v-if="item[0].type == 'image'"></image>
					<image src="@/static/mp3.png" mode="aspectFill" v-else-if="item[0].type == 'audio'"></image>
					<image src="@/static/video.png" mode="aspectFill" v-else-if="item[0].type == 'video'"></image>
					<image src="@/static/file.png" mode="aspectFill" v-else-if="item[0].type == 'file'"></image>
					<image src="@/static/word.png" mode="aspectFill" v-else-if="item[0].type == 'word'"></image>
					<image src="@/static/excel.png" mode="aspectFill" v-else-if="item[0].type == 'excel'"></image>
					<image src="@/static/pdf.png" mode="aspectFill" v-else-if="item[0].type == 'pdf'"></image>
					<image src="@/static/file.png" mode="aspectFill" v-else></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<9">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>

		<view class="sending" @click="sending">
			发送
		</view>
		<view class="cancellation" @click="cancellation">
			取消
		</view>
		<view style="width: 100%;height: 180rpx;"></view>
	</view>
</template>

<script>
	import forum from '@/api/forum.js';
	import {
		img_upload
	} from '@/common/config'
	export default {
		data() {
			return {
				token: '',
				title: '',
				content: '',
				imgList: [],
				img_file: '',
				video_file: '',
				file: '',
				files: [],
				fid: 0,
				TabCur: 0,
				tempFilePaths: [],
			}
		},
		created() {
			const token = uni.getStorageSync('token');
			this.token = token
			this.fid = uni.getStorageSync('fid');
			this.TabCur = uni.getStorageSync('TabCur');
		},
		methods: {
			ChooseImage() {
				uni.chooseImage({
					count: 9, //默认9
					mediaType: ['image', 'video'],
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							this.uploadFile(res.tempFilePaths[i]);
						}
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
					}
				});
				// wx.chooseMessageFile({
				// 	count: 9,
				// 	type: 'all',
				// 	success: (res) => {
				// 		for (let i = 0; i < res.tempFiles.length; i++) {
				// 			const array = {}
				// 			if(this.getFileTypeFromExtension(res.tempFiles[i].name) == 'video'){
				// 				array['url'] = res.tempFiles[i].path
				// 				array['type'] = 'video'
				// 			}else if(this.getFileTypeFromExtension(res.tempFiles[i].name) == 'audio'){
				// 				array['url'] = res.tempFiles[i].path
				// 				array['type'] = 'audio'
				// 			}else if(this.getFileTypeFromExtension(res.tempFiles[i].name) == 'image'){
				// 				array['url'] = res.tempFiles[i].path
				// 				array['type'] = 'image'
				// 			}else if(this.getFileTypeFromExtension(res.tempFiles[i].name) == 'pdf'){
				// 				array['url'] = res.tempFiles[i].path
				// 				array['type'] = 'pdf'
				// 			}else if(this.getFileTypeFromExtension(res.tempFiles[i].name) == 'excel'){
				// 				array['url'] = res.tempFiles[i].path
				// 				array['type'] = 'excel'
				// 			}else if(this.getFileTypeFromExtension(res.tempFiles[i].name) == 'word'){
				// 				array['url'] = res.tempFiles[i].path
				// 				array['type'] = 'word'
				// 			}else{
				// 				array['url'] = res.tempFiles[i].path
				// 				array['type'] = 'file'
				// 			}
				// 			const arr = [array];
				// 			this.tempFilePaths.push(arr)
				// 			this.uploadFile(res.tempFiles[i].path); 
				// 		}
				// 		// if (this.imgList.length != 0) {
				// 		// 	this.imgList = this.imgList.push(this.tempFilePaths)
				// 		// } else {

				// 			this.imgList = this.tempFilePaths

				// 			console.log(this.tempFilePaths)
				// 		// }
				// 	}
				// });
			},
			getFileTypeFromExtension(fileName) {
				console.log(fileName)
				const ext = fileName.slice(((fileName.lastIndexOf(".") - 1) >>> 0) + 2);
				console.log(ext, ext.toLowerCase())
				switch (ext.toLowerCase()) {
					case 'mp4':
					case 'mov':
					case 'avi':
						return 'video';
					case 'mp3':
					case 'wav':
						return 'audio';
					case 'jpg':
					case 'jpeg':
					case 'png':
						return 'image';
					case 'pdf':
						return 'pdf';
					case 'xls':
					case 'xlsx':
						return 'excel';
					case 'doc':
					case 'docx':
						return 'word';
					default:
						return 'file';
				}
			},
			isImageFile(extension) {
				const imageExtensions = ['jpg', 'png', 'bmp', 'jpeg', 'gif', 'webp'];
				return imageExtensions.includes(extension.toLowerCase());
			},
			isVideoFile(extension) {
				const videoExtensions = ['mp4', 'webm'];
				return videoExtensions.includes(extension.toLowerCase());
			},
			uploadFile(filePath) {
				uni.uploadFile({
					url: img_upload,
					filePath: filePath,
					name: 'file',
					formData: {
						'user': 'test',
						'token': this.token
					},
					success: (uploadRes) => {
						const responseObject = JSON.parse(uploadRes.data);
						if (responseObject.code == 403) {
							uni.showToast({
								title: responseObject.msg,
								icon: 'error'
							})
						} else {
							this.files += responseObject.data.url + ','
							const resultArray = this.files.split(',');
							this.files = resultArray
						}
					},
					fail: (error) => {
						console.log(error);
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.files.splice(e.currentTarget.dataset.index, 1)
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			textareaAInput(e) {
				console.log(e)
			},
			cancellation() {
				uni.navigateTo({
					url: '/pages/index/index'
				});
			},
			sending() {
				for (var i = 0; i < this.files.length; i++) {
					const fileExtension = this.files[i].split('.').pop();
					if (this.isImageFile(fileExtension)) {
						this.img_file += this.files[i] + ','
					} else if (this.isVideoFile(fileExtension)) {
						this.video_file += this.files[i] + ','
					} else {
						this.file += this.files[i] + ','
					}
				}
				forum.getWebConfig().then(res => {
					console.log(res)
					if (res.code == 1) {
						forum.articleAdd({
							token: this.token,
							title: this.title,
							content: this.content,
							category_id: this.fid,
							category_type: 'plate',
							post_keywords: '',
							post_excerpt: '',
							post_source: '',
							file: this.file,
							fileImgs: this.img_file,
							fileVideos: this.video_file,
						}).then(res => {
							if (res.code == 1) {
								uni.showToast({
									title: '添加成功',
									icon: 'success'
								})
								uni.setStorageSync('TabCurs', this.TabCur);
								uni.setStorageSync('fid', this.fid);
								uni.redirectTo({
									url: '/pages/index/index'
								});
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	.entire_mid {
		width: 100%;
		height: 100vh;
		background-color: #112858;
	}

	.cu-bar {
		border-bottom: 0;
		z-index: 9990 !important;
	}

	.sending {
		width: 120rpx;
		height: 60rpx;
		background-color: #275EAD;
		border-radius: 16rpx;
		color: #fff;
		line-height: 60rpx;
		text-align: center;
		position: fixed;
		top: 180rpx;
		right: 40rpx;
	}

	.cancellation {
		width: 120rpx;
		height: 60rpx;
		color: #fff;
		line-height: 60rpx;
		text-align: center;
		position: fixed;
		top: 105rpx;
		left: 40rpx;
		z-index: 9999 !important;
	}
</style>